package com.ambimmort.shj.action.policy;

import cn.net.greenet.iprange.info.IpRangeService;
import com.ambimmort.shj.service.policy.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Josh on 2015/7/30.
 */
@Controller("/action/policy/update.do")
public class Update {
    @Autowired
    private PolicyService policyService;

    /**
     *
     * @param request
     * @param response
     * @param type
     * @param instance
     * @param messageNo
     * @param policy
     * @param comment
     * @return
     * @throws IOException
     */

    @RequestMapping("/action/policy/update.do")
    @ResponseBody
    public String update_policy(HttpServletRequest request,HttpServletResponse response, @RequestParam("type") String type,@RequestParam("instance") String instance,@RequestParam("messageNo") String messageNo,@RequestParam("policy") String policy,@RequestParam("comment") String comment) throws IOException {
        HttpSession session = request.getSession(false);
        String username = null;
        try {
            username = session.getAttribute("name").toString();
        } catch (Exception e) {
            response.sendRedirect("/WebManage/404.html");
        }
        //IP地址段入库 ShiYun,2014/11/30
        if (type.equals("0x82")) {
            IpRangeService.save(policy);
        }
        try {
            policyService.update(username, type, instance, messageNo, policy, comment);
                return "{\"result\":\"true\"}";
            } catch (Throwable throwable) {
            throwable.printStackTrace();
            return "{\"result\":\"false\"}";
        }
    }
}
